<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>箭头函数</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="functions.anonymous.html">« 匿名函数</a></li>
      <li style="float: right;"><a href="functions.first_class_callable_syntax.html">First class callable syntax »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.functions.html">函数</a></li>
    <li>箭头函数</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="functions.arrow" class="sect1">
   <h2 class="title">箭头函数</h2>

   <p class="simpara">
    箭头函数是 PHP 7.4 的新语法，是一种更简洁的
    <a href="functions.anonymous.html" class="link">匿名函数</a> 写法。
   </p>
   <p class="simpara">
    匿名函数和箭头函数都是
    <a href="class.closure.html" class="link"><span class="classname"><a href="class.closure.html" class="classname">Closure</a></span></a> 类的实现。
   </p>

   <p class="simpara">
    箭头函数的基本语法为
    <code class="code">fn (argument_list) =&gt; expr</code>。
   </p>

   <p class="simpara">
    箭头函数支持与 <a href="functions.anonymous.html" class="link">匿名函数</a>
    相同的功能，只是其父作用域的变量总是自动的。
   </p>

   <p class="simpara">
    当表达式中使用的变量是在父作用域中定义的，它将被隐式地按值捕获。在下面的例子中，函数
    <var class="varname">$fn1</var> 和 <var class="varname">$fn2</var> 的行为是一样的。
   </p>

   <p class="para">
    <div class="example" id="example-186">
     <p><strong>示例 #1 箭头函数自动捕捉变量的值</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$y&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$fn1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fn</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">)&nbsp;=&gt;&nbsp;</span><span style="color: #0000BB">$x&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$y</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;相当于通过&nbsp;value&nbsp;使用&nbsp;$y：<br /></span><span style="color: #0000BB">$fn2&nbsp;</span><span style="color: #007700">=&nbsp;function&nbsp;(</span><span style="color: #0000BB">$x</span><span style="color: #007700">)&nbsp;use&nbsp;(</span><span style="color: #0000BB">$y</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$x&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$y</span><span style="color: #007700">;<br />};<br /><br /></span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$fn1</span><span style="color: #007700">(</span><span style="color: #0000BB">3</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>以上例程会输出：</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
4
</pre></div>
      </div>
    </div>
   </p>
   <p class="simpara">
    在箭头函数嵌套的情况下同样有效。
   </p>
   <p class="para">
    <div class="example" id="example-187">
     <p><strong>示例 #2 箭头函数自动捕捉变量的值，即使在嵌套的情况下</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$z&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$fn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fn</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">)&nbsp;=&gt;&nbsp;</span><span style="color: #0000BB">fn</span><span style="color: #007700">(</span><span style="color: #0000BB">$y</span><span style="color: #007700">)&nbsp;=&gt;&nbsp;</span><span style="color: #0000BB">$x&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">$y&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$z</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;输出&nbsp;51<br /></span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$fn</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">)(</span><span style="color: #0000BB">10</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="simpara">
    和匿名函数一样，箭头函数语法同样允许标准的函数声明，包括参数和返回类型、缺省值、变量，以及通过引用传递和返回。以下都是箭头函数的有效例子。
   </p>
   <p class="para">
    <div class="example" id="example-188">
     <p><strong>示例 #3 合法的箭头函数例子</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />fn</span><span style="color: #007700">(array&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">)&nbsp;=&gt;&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br />static&nbsp;</span><span style="color: #0000BB">fn</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">int&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">fn</span><span style="color: #007700">(</span><span style="color: #0000BB">$x&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">42</span><span style="color: #007700">)&nbsp;=&gt;&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">fn</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">)&nbsp;=&gt;&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">fn</span><span style="color: #007700">&amp;(</span><span style="color: #0000BB">$x</span><span style="color: #007700">)&nbsp;=&gt;&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">fn</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">,&nbsp;...</span><span style="color: #0000BB">$rest</span><span style="color: #007700">)&nbsp;=&gt;&nbsp;</span><span style="color: #0000BB">$rest</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="simpara">
    箭头函数会自动绑定上下文变量，这相当于对箭头函数内部使用的每一个变量 <var class="varname">$x</var> 执行了一个
    <code class="code">use($x)</code>。这意味着不可能修改外部作用域的任何值，若要实现对值的修改，可以使用
    <a href="functions.anonymous.html" class="link">匿名函数</a> 来替代。
   </p>
   <p class="para">
    <div class="example" id="example-189">
     <p><strong>示例 #4 来自外部范围的值不能在箭头函数内修改</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$x&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$fn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fn</span><span style="color: #007700">()&nbsp;=&gt;&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">++;&nbsp;</span><span style="color: #FF8000">//&nbsp;不会影响&nbsp;x&nbsp;的值<br /></span><span style="color: #0000BB">$fn</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;1<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>

   <div class="sect2">
    <h3 class="title">更新日志</h3>
    <p class="para">
     <table class="doctable informaltable">
      
       <thead>
        <tr>
         <th>版本</th>
         <th>说明</th>
        </tr>

       </thead>

       <tbody class="tbody">
        <tr>
         <td>7.4.0</td>
         <td>
          新增箭头函数语法。
         </td>
        </tr>

       </tbody>
      
     </table>

    </p>
   </div>

   <div class="sect2">
    <h3 class="title">注释</h3>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <span class="simpara">
      可以对箭头函数使用 <span class="function"><a href="function.func-num-args.html" class="function">func_num_args()</a></span>,
      <span class="function"><a href="function.func-get-arg.html" class="function">func_get_arg()</a></span>, 和 <span class="function"><a href="function.func-get-args.html" class="function">func_get_args()</a></span> 函数。
     </span>
    </p></blockquote>
   </div>
  </div></div></div></body></html>